<?php

namespace app\admin\controller\site;

use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;

/**
 * 联系我们
 *
 * @icon fa fa-circle-o
 */
class Contact extends Backend
{

    /**
     * Contact模型对象
     * @var \app\admin\model\site\Contact
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\site\Contact;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function info()
    {
        if($this->request->isAjax()) {
            $contact_name = input('row.contact_name');
            $url = input('row.contact_url');
            $intro = input('row.contact_intro');
            $img = input('row.contact_img');
            // 查询是否已存在数据
            $contact = ConfigModel::where('name', 'contact_name')->where('group', 'contact')->find();
            if (!$contact) {
                $contact = new ConfigModel;
            }
            $contact->name = 'contact_name';
            $contact->group = 'contact';
            $contact->title = 'contact Name';
            $contact->tip = '请填写显示名称';
            $contact->type = 'string';
            $contact->value = $contact_name;
            $contact->save();

            $contact_url = ConfigModel::where('name', 'contact_url')->where('group', 'contact')->find();
            if (!$contact_url) {
                $contact_url = new ConfigModel;
            }
            $contact_url->name = 'contact_url';
            $contact_url->group = 'contact';
            $contact_url->title = 'contact Url';
            $contact_url->tip = '请填写跳转链接';
            $contact_url->type = 'string';
            $contact_url->value = $url;
            $contact_url->save();

            $contact_intro = ConfigModel::where('name', 'contact_intro')->where('group', 'contact')->find();
            if (!$contact_intro) {
                $contact_intro = new ConfigModel;
            }
            $contact_intro->name = 'contact_intro';
            $contact_intro->group = 'contact';
            $contact_intro->title = 'contact Intro';
            $contact_intro->tip = '请填写简介';
            $contact_intro->type = 'string';
            $contact_intro->value = $intro;
            $contact_intro->save();

            $contact_video = ConfigModel::where('name', 'contact_img')->where('group', 'contact')->find();
            if (!$contact_video) {
                $contact_video = new ConfigModel;
            }
            $contact_video->name = 'contact_img';
            $contact_video->group = 'contact';
            $contact_video->title = 'contact Img';
            $contact_video->tip = '请上传介绍视频';
            $contact_video->type = 'string';
            $contact_video->value = $img;
            $contact_video->save();
            $this->success();
        }

        $contact = ConfigModel::where('group', 'contact')->select();
        $data = [
            'contact_name' => ['value' => '' ,'tip' => ''],
            'contact_url' => ['value' => '' ,'tip' => ''],
            'contact_intro' => ['value' => '' ,'tip' => ''],
            'contact_img' => ['value' => '/assets/img/default.png' ,'tip' => ''],
        ];
        if($contact){
            foreach ($contact as $item){
                $data[$item['name']] = [
                    'value' => $item['value'],
                    'tip' => $item['tip'],
                ];
            }

        }
        $this->view->assign('data', $data);
        return $this->fetch('info');
    }
}
